var app = angular.module('zhijiaoAdminApp.controllers');

app.controller('adminCtr', ['$rootScope', '$scope', 'ajaxService', 'layerService', 'toastPCService', '$timeout', 'tempDataService', 'session',
    function ($rootScope, $scope, ajax, layer, toast, timeout, temp, session) {
        var edit = function () {
            return {
                auth_id: 0,
                name: '',
                cover: '',
                username: '',
                password: '',
                status: 1
            };
        };

        $scope.data = {
            roleList: [],
            list: [],
            authList: [],
            edit: edit(),
            pages: 1,
            count: 0,
            search: {
                page: 1,
                pageSize: 15,
                keyword: '',
                auth_id: 0
            }
        };

        $scope.getAuthList = function () {
            $scope.data.authList = [{id: 0, name: '选择角色'}];
            $scope.data.allAuth.forEach(function (item) {
                if (item.type == $scope.data.edit.type) {
                    $scope.data.authList.push(item)
                }
            })
        }


        if ($scope.data.authList.length == 0) {
            ajax('commonAuthlist', {}, function (rel) {
                $scope.data.allAuth = rel.data;
                $scope.getAuthList();
            })
        }

        $scope.getList = function (page) {
            if (page) {
                $scope.data.search.page = page;
            }
            ajax('getAdminList', $scope.data.search, function (rel) {
                if (rel.code) {
                    toast.show(2, rel.message);
                } else {
                    $scope.data.list = rel.data.data;
                    $scope.data.pages = rel.data.last_page;
                    $scope.data.count = rel.data.total;
                }
            })
        }

        $scope.getAuthName = function (authId) {
            var auth = $scope.data.allAuth;
            for (i in auth) {
                if (auth[i]['id'] == authId) {
                    return auth[i]['name']
                }
            }
        }

        $scope.edit = function (item) {
            $scope.data.viewImg = false;
            if (item) {
                $scope.data.edit = angular.copy(item);
                item.view_cover = '';
                $scope.data.viewImg = true;
            } else {
                $scope.data.edit = edit();
                $scope.data.viewImg = true;
            }
            timeout(function () {
                $('#myModal label.error').remove();
                $('#myModal .error').removeClass('error');
                $('#myModal').modal("show");
            }, 200);
        }

        $scope.commit = function () {
            if ($('#editForm').valid()) {
                ajax('saveAdmin', $scope.data.edit, function (rel) {
                    if (!rel.code) {
                        toast.show(1, '保存成功');
                        $scope.getList();
                        $('#myModal').modal('hide');
                    }
                })
            }
        }

        $scope.del = function (item) {
            layer.confirm('确定删除【' + item.name + '】', ['确定', '取消'], function () {
                ajax('delAdmin', {id: item.id}, function (rel) {
                    if (!rel.code) {
                        toast.show(1, '删除成功');
                        $scope.getList();
                    }
                })
            })
        }


    }
])